package org.dandelion.calendar;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.graphics.Typeface;
import android.os.Bundle;

import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.hb.dialog.myDialog.MyAlertInputDialog;

import org.dandelion.R;

public class CalenderActivity extends AppCompatActivity implements View.OnClickListener {
    private GridView record_gridView;//定义gridView
    private DateAdapter dateAdapter;//定义adapter
    private ImageView record_left;//左箭头
    private ImageView record_right;//右箭头
    private TextView record_title;//标题
    private TextView w1, w2, w3, w4, w5, w6, w7;
    private LinearLayout back;
    private Button btn_change;
    private int year;
    private int month;
    private String title;
    private int[][] days = new int[6][7];
    private int[] day = new int[42];

    private final int[] backId = {R.drawable.back1, R.drawable.back2, R.drawable.back3,
            R.drawable.back4, R.drawable.back5, R.drawable.back6, R.drawable.back7,
            R.drawable.back8, R.drawable.back9
    };

    int backmum = 0;

    private final int[] icon = {R.drawable.ic_chibrush, R.drawable.ic_chrope,
            R.drawable.ic_fan, R.drawable.ic_pipa, R.drawable.ic_tuanshan,
            R.drawable.ic_umbrella, R.drawable.ic_zhujian
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar);
        initData();
        record_left = findViewById(R.id.record_left);
        record_right = findViewById(R.id.record_right);
        record_title = findViewById(R.id.record_title);
        btn_change = findViewById(R.id.btn);
        back = findViewById(R.id.back);
        w1 = findViewById(R.id.mon);
        w2 = findViewById(R.id.tue);
        w3 = findViewById(R.id.wed);
        w4 = findViewById(R.id.thu);
        w5 = findViewById(R.id.fri);
        w6 = findViewById(R.id.sat);
        w7 = findViewById(R.id.sun);
        back.setBackgroundResource(backId[0]);
        title = year + "年" + month + "月";
        record_title.setText(title);
        record_gridView = findViewById(R.id.record_gridView);
        Typeface typeface = Typeface.createFromAsset(getAssets(), "maobi.ttf");
        record_title.setTypeface(typeface);
        w1.setTypeface(typeface);
        w2.setTypeface(typeface);
        w3.setTypeface(typeface);
        w4.setTypeface(typeface);
        w5.setTypeface(typeface);
        w6.setTypeface(typeface);
        w7.setTypeface(typeface);
        btn_change.setTypeface(typeface);
        days = DateUtils.getDayOfMonthFormat(DateUtils.getYear(), DateUtils.getMonth());
        //将天数转为一维
        int dayNum = 0;

        for (int[] ints : days) {
            for (int anInt : ints) {
                day[dayNum] = anInt;
                dayNum++;
            }
        }

        DayDao dayDao = new DayDao(CalenderActivity.this);
        String[] signDay = dayDao.findbyMonth(month);
        dateAdapter = new DateAdapter(this, days, year, month, signDay);//传入当前月的年
        record_gridView.setAdapter(dateAdapter);
        record_gridView.setOnItemClickListener((adapterView, view, position, l) -> {
            Toast.makeText(CalenderActivity.this, "天数:" + day[position] +
                    "月数" + month, Toast.LENGTH_SHORT).show();
            final int dataDay = day[position];
            boolean signFlag = true;
            DayDao dayDao14 = new DayDao(CalenderActivity.this);
            String[] signDay1 = dayDao14.findbyMonth(month);

            for (int i = 0; i < 32; i++) {
                if (Integer.toString(day[position]).equals(signDay1[i])) {
                    signFlag = false;
                }
            }

            if (position < 7 && day[position] > 20) {
            } else if (position > 20 && day[position] < 15) {
            } else {
                if (signFlag) {
                    final MyAlertInputDialog myAlertInputDialog = new MyAlertInputDialog(
                            CalenderActivity.this).builder().setTitle("请输入")
                            .setEditText("");
                    myAlertInputDialog.setPositiveButton("确认", v -> {
                        if (myAlertInputDialog.getResult().equals("")) {
                            Toast.makeText(CalenderActivity.this, "信息为空",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            DayDao dayDao1 = new DayDao(CalenderActivity.this);
                            dayDao1.add(Integer.toString(dataDay).trim(),
                                    Integer.toString(month).trim(),
                                    myAlertInputDialog.getResult().trim());
                            updatepage();
                        }
                        myAlertInputDialog.dismiss();
                    }).setNegativeButton("取消", v -> {
                        Toast.makeText(CalenderActivity.this, "quexiao",
                                Toast.LENGTH_SHORT).show();
                        myAlertInputDialog.dismiss();
                    });
                    myAlertInputDialog.show();
                } else {
                    String str = dayDao14.findbyMonthDay(month, dataDay);
                    //添加"Yes"按钮
                    //添加取消
                    //添加普通按钮
                    AlertDialog alertDialog2 = new AlertDialog.Builder(CalenderActivity.this)
                            .setTitle("特殊日子")
                            .setMessage(str)
                            .setIcon(icon[dataDay % 7])
                            .setPositiveButton("确定", (dialogInterface, i) -> {
                            }).setNegativeButton("删除", (dialogInterface, i) -> {
                                DayDao dayDao12 = new DayDao(CalenderActivity.this);
                                dayDao12.delete(dayDao12.findIdbyMonthDay(month, dataDay));
                                updatepage();
                            }).setNeutralButton("编辑", (dialogInterface, i) -> {
                                final MyAlertInputDialog myAlertInputDialog =
                                        new MyAlertInputDialog(CalenderActivity.this)
                                                .builder()
                                                .setTitle("请输入")
                                                .setEditText("");
                                myAlertInputDialog.setPositiveButton("确认", v -> {
                                    Toast.makeText(CalenderActivity.this, "wenben:"
                                                    + myAlertInputDialog.getResult(),
                                            Toast.LENGTH_SHORT).show();
                                    DayDao dayDao13 = new DayDao(CalenderActivity.this);
                                    dayDao13.update(month, dataDay, myAlertInputDialog.getResult().trim());
                                    updatepage();
                                    myAlertInputDialog.dismiss();
                                }).setNegativeButton("取消", v -> {
                                    Toast.makeText(CalenderActivity.this,
                                            "quexiao", Toast.LENGTH_SHORT).show();
                                    myAlertInputDialog.dismiss();
                                });
                                myAlertInputDialog.show();
                            }).create();
                    alertDialog2.show();
                }
            }
        });
        record_left.setOnClickListener(this);
        record_right.setOnClickListener(this); //换月点击监听事件
    }

    private void initData() {
        year = DateUtils.getYear();
        month = DateUtils.getMonth();
    }

    private int[][] nextMonth() { //下月
        if (month == 12) {
            month = 1;
            year++;
        } else {
            month++;
        }
        days = DateUtils.getDayOfMonthFormat(year, month);
        return days;
    }

    private int[][] preMonth() { //上月
        if (month == 1) {
            month = 12;
            year--;
        } else {
            month--;
        }
        days = DateUtils.getDayOfMonthFormat(year, month);
        return days;
    }

    private void setTitle() {
        title = year + "年" + month + "月";
        record_title.setText(title);
    }

    public void updatepage() {
        DayDao dayDao = new DayDao(CalenderActivity.this);
        String[] signDay = dayDao.findbyMonth(month);
        if (year != DateUtils.getYear()) {
            for (int j = 0; j < signDay.length - 1; j++) {
                signDay[j] = "";
            }
        }
        dateAdapter = new DateAdapter(this, days, year, month, signDay);
        record_gridView.setAdapter(dateAdapter);
        dateAdapter.notifyDataSetChanged();
    }

    public void changBackGround(View view) {
        backmum++;
        if (backmum >= backId.length) {
            backmum = 0;
        }
        back.setBackgroundResource(backId[backmum]);
    }

    @Override
    public void onClick(View view) {
        DayDao dayDao = new DayDao(CalenderActivity.this);
        switch (view.getId()) {
            case R.id.record_left:
                days = preMonth();
                int dayNum = 0;
                for (int i = 0; i < days.length; i++) {
                    for (int j = 0; j < days[i].length; j++) {
                        day[dayNum] = days[i][j];
                        dayNum++;
                    }
                }
                updatepage();
                setTitle();
                break;
            case R.id.record_right:
                days = nextMonth();
                dayNum = 0;

                for (int[] ints : days) {
                    for (int j = 0; j < ints.length; j++) {
                        day[dayNum] = ints[j];
                        dayNum++;
                    }
                }

                updatepage();
                setTitle();
                break;
        }
    }
}
